package com.example.chartroom.mapper;

import com.example.chartroom.model.Friend;
import com.example.chartroom.model.MessageSession;
import com.example.chartroom.model.MessageSessionUser;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Mapper
public interface MessageSessionMapper {
    //通过用户Id查询会话Id
    @Select("select sessionId from message_session where sessionId in " +
            "(select sessionId from message_session_user where userId = #{userId})" +
            "order by lastTime desc")
    List<Integer> selectSessionIdByUserId(int userId);

    //通过会话Id查询用户,除了自己
    @Select("select userId as friendId, username as friendName from user where userId in" +
            "(select userId from message_session_user " +
            "where sessionId = #{sessionId} and userId != #{selfUserId})")
    List<Friend> selectFriendsBySessionId(@Param("sessionId") int sessionId, @Param("selfUserId") int selfUserId);

    //新增会话记录
    int addMessageSession(MessageSession messageSession);

    //给message_session_user增加记录
    int addMessageSessionUser(MessageSessionUser messageSessionUser);
}
